package com.finger.forest.ui.my;

import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.finger.forest.AutoPhotoActivity;
import com.finger.forest.CommunicationActivity;
import com.finger.forest.ConsultationActivity;
import com.finger.forest.EquipmentManagerActivity;
import com.finger.forest.OfflineMapActivity;
import com.finger.forest.OfflineMapDataActivity;
import com.finger.forest.OfflineMapDetailActivity;
import com.finger.forest.R;
import com.finger.forest.SettingActivity;
import com.finger.forest.SignActivity;
import com.finger.forest.SynchronizationDataActivity;
import com.finger.forest.UserDetailActivity;
import com.finger.forest.UserManagerActivity;
import com.finger.forest.WorkLogActivity;
import com.finger.forest.base.BaseApi;
import com.finger.forest.base.BaseFragment;
import com.finger.forest.base.BaseView;
import com.finger.forest.base.CommonKey;
import com.finger.forest.base.ResponseTag;
import com.finger.forest.custom.MyImageView;
import com.finger.forest.custom.XCRoundImageView;
import com.finger.forest.dialog.SoSDialog;
import com.finger.forest.http.presenter.RegisterPresenter;
import com.finger.forest.model.GridUserModel;
import com.finger.forest.model.MobileUserModel;
import com.finger.forest.utils.ClickUtils;
import com.finger.forest.utils.RequestUtil;
import com.finger.forest.utils.StringUtil;
import com.finger.forest.utils.ToastUtils;
import com.finger.forest.utils.encode.PrefsUtil;
import com.google.gson.JsonObject;
import com.jakewharton.rxbinding2.view.RxView;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 我的
 */
public class MyFragment extends BaseFragment<RegisterPresenter> implements BaseView {

    private Unbinder unbinder;

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeNavigation() {

    }

    @BindView(R.id.btn_right)
    ImageView rightIv;
    //账号
    @BindView(R.id.tv_header)
    TextView headerTv;
    //打卡
    @BindView(R.id.tv_sign)
    TextView signTv;
    //SOS求救
    @BindView(R.id.tv_SOS)
    TextView sosTv;
    //即时通讯
    @BindView(R.id.tv_communication)
    TextView communicationTv;

    //设备管理
    @BindView(R.id.tv_equipment_manager)
    TextView equipmentManagerTv;
    //数据同步
    @BindView(R.id.tv_data_sync)
    TextView dataSyncTv;
    //同步会商
    @BindView(R.id.tv_consultation)
    TextView consultationTv;
    //设置
    @BindView(R.id.tv_setting)
    TextView settingTv;
    //设置
    @BindView(R.id.iv_header)
    MyImageView headerIv;
    //用户管理
    @BindView(R.id.tvUserManager)
    TextView tvUserManager;
    //离线地图
    @BindView(R.id.tv_offline_map)
    TextView offlineMapTv;

    private SoSDialog soSDialog;

    @Override
    protected void initializeView() {

        //获取个人详情和头像
        getUserDetail();

        clicks();
    }

    @Override
    protected void loadDataFromServer() {

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, root);
        return root;
    }

    private void getUserDetail() {
        String userId = PrefsUtil.getString(this.getContext(), CommonKey.USER_ID);
        Map<String, Object> maps = new HashMap<>();
        maps.put("userId", userId);

        presenter.loadPersonInfo(maps);
    }


    private void clicks() {
        //跳转工作日志
        RxView.clicks(rightIv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intentWorkLog = new Intent(getActivity(), WorkLogActivity.class);
            startActivity(intentWorkLog);
        });

        //跳转打卡
        RxView.clicks(signTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intentSign = new Intent(getActivity(), SignActivity.class);
            startActivity(intentSign);
        });

        //跳转个人详情
        RxView.clicks(headerIv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intentUserDetail = new Intent(getActivity(), UserDetailActivity.class);
            startActivity(intentUserDetail);
        });

        //跳转即时通讯
        RxView.clicks(communicationTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intentCommunitation = new Intent(getActivity(), CommunicationActivity.class);
            startActivity(intentCommunitation);
        });

        //跳转设备管理
        RxView.clicks(equipmentManagerTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intentequipmentManager = new Intent(getActivity(), EquipmentManagerActivity.class);
            startActivity(intentequipmentManager);
        });

        //跳转设置
        RxView.clicks(settingTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intentSetting = new Intent(getActivity(), SettingActivity.class);
            startActivity(intentSetting);
        });

        //数据同步
        RxView.clicks(dataSyncTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intent = new Intent(getActivity(), SynchronizationDataActivity.class);
            startActivity(intent);
        });

        //同步会商
        RxView.clicks(consultationTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intent = new Intent(getActivity(), ConsultationActivity.class);
            startActivity(intent);
        });

        //用户管理
        RxView.clicks(tvUserManager).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intent = new Intent(getActivity(), UserManagerActivity.class);
            startActivity(intent);
        });

        //离线地图
        RxView.clicks(offlineMapTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Intent intent = new Intent(getActivity(), OfflineMapDetailActivity.class);
            startActivity(intent);
        });

        //SOS求救
        RxView.clicks(sosTv).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            if (null == soSDialog) {
                soSDialog = new SoSDialog(getActivity(), R.style.dialogFullStyle, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        requestPermission(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x001);
                    }
                });
            }
            soSDialog.show();
        });
    }

    @Override
    public void permissionSuccess(int requestCode) {
        if (requestCode == 0x001) {
            Intent intent = new Intent(getActivity(), AutoPhotoActivity.class);
            startActivity(intent);

            soSDialog.dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != unbinder) {
            unbinder.unbind();
        }
    }

    @Override
    public void onSuccess(Object model, int tag, String msg) {
        switch (tag) {
            case ResponseTag.CHECK_PERSON_INFO:
                GridUserModel userModel = (GridUserModel) model;
                if (null != userModel) {
                    headerTv.setText(userModel.getName());
                    if (!StringUtil.isEmpty(userModel.getHeadPortraitRul(), true)) {
                        headerIv.setImageURL(userModel.getHeadPortraitRul());
                    }
                }
                break;
        }
    }

    @Override
    public void onError(int errcode, int tag, String message) {
        ToastUtils.show(message);
    }
}